﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.Revit.Electric.Common
{
	public class HYFamilyUtils
	{
		public static FamilySymbol FamilySymbol(Autodesk.Revit.DB.Document doc, string familyFile, BuiltInCategory category)
		{
			if (doc == null || !File.Exists(familyFile))
			{
				return null;
			}
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyFile);
			Family family = null;
			Transaction transaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), category))
				{
					FamilySymbol familySymbol = element as FamilySymbol;
					if (familySymbol.Family.Name == fileNameWithoutExtension)
					{
						return familySymbol;
					}
				}
				transaction = new Transaction(doc, "LoadFamily");
				transaction.Start();
				doc.LoadFamily(familyFile, out family);
				transaction.Commit();
			}
			catch (Exception ex)
			{
				TaskDialog.Show("盈建科提示", ex.Message);
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			if (family == null)
			{
				return null;
			}
			FamilySymbol result = null;
			using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
			{
				if (enumerator2.MoveNext())
				{
					result = enumerator2.Current;
				}
			}
			return result;
		}
	}
}
